package com.ling.service;

import com.ling.bean.UserDownvoteBlog;
import com.ling.bean.UserDownvoteBlogExample;
import com.ling.dao.UserDownvoteBlogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserDownvoteBlogService {
    private final UserDownvoteBlogMapper userDownvoteBlogMapper;

    @Autowired
    public UserDownvoteBlogService(UserDownvoteBlogMapper userDownvoteBlogMapper) {
        this.userDownvoteBlogMapper = userDownvoteBlogMapper;
    }

    //查询是否已经反对过指定id的blog
    public boolean hasDownvoted(UserDownvoteBlog userDownvoteBlog) {
        UserDownvoteBlogExample userDownvoteBlogExample = new UserDownvoteBlogExample();
        userDownvoteBlogExample.createCriteria().andUseridEqualTo(userDownvoteBlog.getUserid()).andDownvoteblogidEqualTo(userDownvoteBlog.getDownvoteblogid());
        List<UserDownvoteBlog> userDownvoteBlogs = userDownvoteBlogMapper.selectByExample(userDownvoteBlogExample);
        return !userDownvoteBlogs.isEmpty();
    }

    //保存记录
    public void saveDownvote(UserDownvoteBlog userDownvoteBlog) {
        userDownvoteBlogMapper.insertSelective(userDownvoteBlog);
    }
}
